package com.zahb.qadx.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.skeleton.RecyclerViewSkeletonScreen;
import com.zahb.qadx.skeleton.Skeleton;
import com.zahb.qadx.ui.activity.CertificateDetailActivity;
import com.zahb.qadx.ui.adapter.CertificateAdapter;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class CertificateListFragment extends BaseFragmentExt implements OnRefreshLoadMoreListener {


    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;

    private CertificateAdapter certificateAdapter;
    private RecyclerViewSkeletonScreen mSkeletonScreen;
    private long mSkeletonShowTime;
    @Override
    protected int getLayout() {
        return R.layout.fragment_train_task_list;
    }

    @Override
    protected void initViews(View rootView) {
        mUnbinder = ButterKnife.bind(this, rootView);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setEnableLoadMore(true);
        mRefreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        mRefreshLayout.setOnRefreshLoadMoreListener(this);



        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mRecyclerView.getLayoutParams();
        layoutParams.setMarginStart(0);
        layoutParams.setMarginEnd(0);
        mRecyclerView.setLayoutParams(layoutParams);
        certificateAdapter = new CertificateAdapter(R.layout.item_certificate);
        certificateAdapter.type = belong;
        mRecyclerView.setAdapter(certificateAdapter);

        certificateAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Intent intent = new Intent(getActivity(), CertificateDetailActivity.class);
            intent.putExtra("data",certificateAdapter.getItem(position).getCertificateUserId());
            startActivity(intent);
        });

        View emptyView = View.inflate(getContext(), R.layout.empty_layout, null);
        emptyView.setVisibility(View.VISIBLE);
        certificateAdapter.setEmptyView(emptyView);

        //getCertificateList();
    }

    private boolean mFirstLoad = true;

    @Override
    public void onResume() {
        super.onResume();
        if (mFirstLoad) {
            mFirstLoad = false;
            onRefresh(mRefreshLayout);
            showSkeleton();
        }
    }

    private void showSkeleton() {
        mRefreshLayout.setEnableRefresh(false);
        mSkeletonScreen = Skeleton.bind(mRecyclerView)
                .adapter(certificateAdapter)
                .shimmer(true)
                .shimmerMode(RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM)
                .angle(15)
                .frozen(true)
                //.color(R.color.black_333333)
                .duration(1000)
                .count(10)
                .load(R.layout.item_certificate_skeleton)
                .show();
        mSkeletonShowTime = System.currentTimeMillis();
    }

    private void hideSkeleton() {
        if (mSkeletonScreen != null) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - mSkeletonShowTime > Constant.MIN_NETWORK_TIME) {
                mRefreshLayout.setEnableRefresh(true);
                mSkeletonScreen.hide();
                mSkeletonScreen = null;
            } else {
                addDisposable(Observable.timer(mSkeletonShowTime + Constant.MIN_NETWORK_TIME - currentTime, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aLong -> hideSkeleton()));
            }
        }
    }

    public int belong=0;
    private int pageNum=1;
    private static final int pageSiz=10;
    private int totalPage=1;
    private void getCertificateList() {
        addDisposable(RetrofitService.getNetService()
                .getCertificateList(belong,pageNum,pageSiz)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        totalPage = response.getData().getPages();
                        if (pageNum == 1) {
                            certificateAdapter.setList(response.getData().getList());
                        } else {
                            certificateAdapter.addData(response.getData().getList());
                        }
                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                    mRefreshLayout.finishLoadMore();
                    mRefreshLayout.finishRefresh();
                    hideSkeleton();
                }, throwable -> {
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                    mRefreshLayout.finishLoadMore();
                    mRefreshLayout.finishRefresh();
                    hideSkeleton();
                }));
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if(pageNum<totalPage){
            pageNum++;
            getCertificateList();
        }else{
            mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum=1;
        getCertificateList();
    }
}
